<?php

namespace app\index\model;

use think\Model;
use think\Db;

class User extends Model
{
    protected $insert = ['status' => 0];
    //新增用戶
    public function createuser($param)
    {
    	$phone = User::get(['phone'=>$param['phone']]);
    	$email = User::get(['email'=>$param['email']]);
    	if(isset($phone)){
    		return 'phonealr';
    	}
    	if(isset($email)){
    		return 'emailealr';
    	}
    	$this->startTrans();
    	try{
    		$param['password'] = pwd_md5($param['password']);
    		$this->data($param)->allowField(true)->save();
            $userinfo['username'] = $param['username'];
            $userinfo['phone'] = $param['phone'];
            $userinfo['email'] = $param['email'];
            Db::name('resume')->insertAll($userinfo);
    		$this->commit();
    		return true;
    	}catch(\Exception $e){
    		$this->rollback();
    		$this->error = '添加失败';
    		return false;
    	}
    }

    //用戶登陸
 	public function userlogin($param)
 	{
    	$email = User::get(['email'=>$param['email']]);
 		if(!isset($email)){
            $this->error = '此账户不存在!';
    		return false;
    	}
    	if($email['password']!=pwd_md5($param['password'])){
            $this->error = '用户密码错误!';
            return false;
    	}
    	$this->startTrans();
    	try{
    		$userinfo['account'] = $param['email'];
    		$userinfo['ip'] = $param['ip'];
            $data['id'] = $email['id'];
            $data['username'] = $email['username'];
    		Db::name('logininfo')->insertAll($userinfo);
            $this->commit();
    		return true;
    	}catch(\Exception $e){
    		$this->rollback();
            $this->error = '登录失败!';
            return false;
    	}
 	}   
}
